package com.hzw.saas.api.rbac.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 权限类型
 *
 * @author zzl
 * @since 08/30/2021
 */
public enum PermTypeEnum {

    MENU(1, "菜单"),
    BUTTON(2, "按钮"),
    DATA(3, "数据"),
    ;
    private final Integer code;
    private final String text;

    PermTypeEnum(Integer code, String text) {
        this.code = code;
        this.text = text;
    }

    public Integer getCode() {
        return code;
    }

    public String getText() {
        return text;
    }

    public static String getText(Integer code) {
        PermTypeEnum instance = instance(code);
        return Objects.isNull(instance) ? "" : instance.getText();
    }

    public static void assertValid(Integer code) {
        PermTypeEnum instance = instance(code);
        Assert.notNull(instance, "权限类型不存在: {}", code);
    }

    public static PermTypeEnum instance(Integer code) {
        for (PermTypeEnum value : PermTypeEnum.values()) {
            if (Objects.equals(value.getCode(), code)) {
                return value;
            }
        }
        return null;
    }

    public static PermTypeEnum assertInstance(Integer code) {
        PermTypeEnum instance = instance(code);
        Assert.notNull(instance, "权限类型不存在: {}", code);
        return instance;
    }
}
